package com.yopuo.port.util;

import com.yopuo.common.utils.sign.Md5Utils;
import lombok.extern.slf4j.Slf4j;

/**
 * 从云盘跳转过来免登录工具类
 */
@Slf4j
public class UserCodeUtil {


    // 30秒内的请求有效
    private static final Long exprireTime = 30*1000L;

    // 颜值
    private static final String salt= "tiannengzhenbang";

    public static final boolean checkUserAuthCode(String code,long timestamp,Integer userId){
        String c = userId+":"+salt+":"+timestamp;
        c = Md5Utils.hash(c);
        if(!c.equalsIgnoreCase(code)){
            return false;
        }
        if(Math.abs(timestamp-System.currentTimeMillis())>exprireTime){
            log.info("当前授权code已经过期！！");
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(checkUserAuthCode( "740CD43AF9FBB2979020D0184CBAC421", 1608605183084L,123));
    }

}
